﻿using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour
{	
	[SerializeField]
	private Transform _crosshair;
	
	[SerializeField]
	private BeeAnimator _bee;
	
	[SerializeField]
	private GameObject _nektarLinker;
	
	[SerializeField]
	private GameObject _bullet;
	
	[SerializeField]
	private NektarMeter _nektarMeter;
	
	private Vector3 _tmpCrosshairPos;
	private Vector3 _tmp;	
	
	private Transform _transform;
	private Rigidbody _rigidbody;
	private bool _isFirstHit;
	private int _ammo;
	
	private Vector3 _velocity;
	private Vector3 _targetVelocity;
	private Vector3 _velocityDelta;
	private float _maxVelocityChange = 0.5f;
	private float _speed = 5.0f;
	
	private NektarLinker _activeNektarLinker;
	
	
	public Vector3 GetCrossHairPos()
	{
		return _tmpCrosshairPos;
	}

	public void AddLinkToActive()
	{
		
	}
	
	private int _orgAmmo;
	
	public bool ModifyAmmo(int amount)
	{
		_orgAmmo = _ammo;
		_ammo += amount;				
		
		if(_ammo <= 0)
		{
			_ammo = _orgAmmo;
			_nektarMeter.UpdateMeter(_ammo, 200); //DANGER!
			return false;
		}
		else
		{
			_nektarMeter.UpdateMeter(_ammo, 200); //DANGER!
			return true;
		}
	}
	
	public void RegisterMiss()
	{
		_isFirstHit = false;
		//Destroy(_activeNektarLinker.gameObject);
	}
	
	public void RegisterHit(Transform target)
	{
		if(_isFirstHit == true)
		{
			_activeNektarLinker.AddLink(target);
			_isFirstHit = false;
		}
		else
		{
			_isFirstHit = true;
			_activeNektarLinker = CreateNewNektarLinker();
			_activeNektarLinker.AddLink(target);
		}
	}
	
	private NektarLinker CreateNewNektarLinker()
	{
		GameObject tmp = GameObject.Instantiate(_nektarLinker, _transform.position, Quaternion.identity) as GameObject;
		return tmp.GetComponent<NektarLinker>();
	}
	
	private IEnumerator HitTimeout()
	{
		yield return new WaitForSeconds(1.5f);
		_isFirstHit = false;
	}
	
	private void Awake()
	{
		_transform = transform;
		_rigidbody = rigidbody;
	}
	
	int _toKill;
	int _kills;
	
	public void AddKill()
	{
		_kills++;
	}
	
	void Start()
	{
		_ammo = 50;
		_nektarMeter.UpdateMeter(_ammo, 200); //DANGER!
		
		_toKill = Dragonfly.GetCounter();
	}
	
	void Update()
	{
		_tmp = Input.mousePosition; 
		_tmp.z = 9.0f; // is y distance of camera
		_tmpCrosshairPos = Camera.main.ScreenToWorldPoint(_tmp);
		_tmpCrosshairPos.y = 0.0f;
		_crosshair.position = _tmpCrosshairPos;
		
		if(_kills >= _toKill)
		{
			//VICTYORY!
			PlayerPrefs.SetString("GameOverText", "Victory");
			Application.LoadLevel(1);
		}
		
		
		if(Input.GetMouseButtonDown(0))
		{
			OnLeftClick();
		}
		
		if(Input.GetMouseButtonDown(1))
		{
			OnRightClick();
		}
		
		_targetVelocity = Vector3.zero;
		
		if(Input.GetKey(KeyCode.A))
		{
			_targetVelocity += Vector3.left;
		}
		
		if(Input.GetKey(KeyCode.D))
		{
			_targetVelocity += Vector3.right;
		}
		
		if(Input.GetKey(KeyCode.W))
		{
			_targetVelocity += Vector3.forward;
		}
		
		if(Input.GetKey(KeyCode.S))
		{
			_targetVelocity += Vector3.back;
		}
		
		_bee.UpdateLookRotation(_tmpCrosshairPos);
	}
	
	private void FixedUpdate()
	{
		//_targetVelocity = new Vector3(0.0f, 0.0f, 1.0f);
		_targetVelocity = transform.TransformDirection(_targetVelocity);
		_targetVelocity *= _speed;

		_velocity = _rigidbody.velocity;

		_velocityDelta = (_targetVelocity - _velocity);
		_velocityDelta.x = Mathf.Clamp(_velocityDelta.x, -_maxVelocityChange, _maxVelocityChange);
		_velocityDelta.z = Mathf.Clamp(_velocityDelta.z, -_maxVelocityChange, _maxVelocityChange);
		_velocityDelta.y = 0;

		_rigidbody.AddForce(_velocityDelta, ForceMode.VelocityChange);
	}
	
	private void OnLeftClick()
	{
		ShootNormalProjectile();
	}
	
	private void OnRightClick()
	{
		
	}
	
	private void ShootNormalProjectile()
	{
		if(_ammo >= 3)
		{
			ModifyAmmo(-3);
			Instantiate(_bullet, _transform.position, Quaternion.Euler(new Vector3(0.0f, _bee.GetRotationY(), 0.0f)));
			AudioSource.PlayClipAtPoint(Resources.Load("Sounds/Shoot2") as AudioClip, transform.position);
		}
		else
		{
			//display error?
		}	
	}
	
	private void OnCollisionEnter(Collision col)
	{
		if(col.transform.tag == "enemy")
		{
			PlayerPrefs.SetString("GameOverText", "Defeat");
			Application.LoadLevel(1);
		}
	}
}
